<html>
<head>
<title>Random Number Generator Test Page</title>
<script src="namespace.js"></script>
<script src="base.js"></script>
<script src="mersene_twister.js"></script>
<script src="random.js"></script>
<script src="timer.js"></script>
<script src="unit.js"></script>
</head>
<body>
<h1><script>document.write(document.title);</script></h1>
<SCRIPT>
var UT = global_namespace.Import('startpad.unit');
var ts = new UT.TestSuite();
ts.DWOutputDiv();
</script>
<script>
var MT = global_namespace.Import('startpad.random.mt');
var Random = global_namespace.Import('startpad.random');

ts.AddTest("Mersene Int32", function(ut)
{
	var expected = [
		1067595299,  955945823,  477289528, 4107218783, 4228976476,
		3344332714, 3355579695, 227628506,  810200273, 2591290167,
		2560260675, 3242736208,  646746669, 1479517882,4245472273
		];
		
	MT.init_by_array([0x123, 0x234, 0x345, 0x456]);
	
	for (var i = 0; i < expected.length; i++)
		{
		ut.AssertEq(MT.genrand_int32(), expected[i]);
		}
});

ts.AddTest("Mersene Real1", function(ut)
{
	var expected = [
		0.24856890, 0.22257348, 0.11112763, 0.95628639, 0.98463532,
		0.77866314, 0.78128178, 0.05299889, 0.18863945, 0.60333176,
		0.59610714, 0.75500836, 0.15058244, 0.34447710, 0.98847604,
		0.26621219, 0.89958089, 0.74995262, 0.41295089, 0.26512361
		];
		
	MT.init_by_array([0x123, 0x234, 0x345, 0x456]);
	
	for (var i = 0; i < expected.length; i++)
		{
		ut.AssertLT(Math.abs(MT.genrand_real1()-expected[i]), 0.00000001);
		}
});

ts.AddTest("Mersene Res53", function(ut)
{
	var expected = [
	0.2485689015878, 0.1111276295504, 0.9846353141864, 0.7812817771211, 0.1886394515882,
	0.5961071458087, 0.1505824427640, 0.9884760399626, 0.8995808865871, 0.4129508828008,
	0.3310613579726, 0.6658802808137, 0.9021936127785, 0.0168917816510, 0.6054105963840,
	0.6502730486458, 0.1410585423049, 0.1665058342209, 0.3080269246159, 0.3248199587299,
	0.4893466447929, 0.6869515209418, 0.6741796549553, 0.2337834128975, 0.4915619250667
	];
	
	MT.init_by_array([0x123, 0x234, 0x345, 0x456]);
	
	for (var i = 0; i < expected.length; i++)
		{
		ut.AssertLT(Math.abs(MT.genrand_res53()-expected[i]), 0.00000001);
		}
});

ts.AddTest("random (real)", function(ut)
{
	for (var i = 0; i < 20; i++)
		{
		var r = Random.random();
		ut.Assert(r >= 0 && r <= 1);
		}
});

ts.AddTest("randomint", function(ut)
{
	Random.seed();
	for (var i = 0; i < 20; i++)
		{
		var r = Random.randint(0,100);
		ut.Assert(Math.floor(r) == r);
		ut.Assert(r >= 0 && r <= 100);
		}
});

ts.AddTest("seed", function(ut)
{
	var seeds = [123, 0.1234, "hello", new Date(), [123, 456]];
	for (var i = 0; i < seeds.length; i++)
		{
		var save = []
		Random.seed(seeds[i]);
		for (var j = 0; j < 30; j++)
			{
			save.push(Random.random())
			}
			
		Random.seed(seeds[i]);
		for (var j = 0; j < 30; j++)
			{
			ut.AssertEq(Random.random(), save[j]);
			}
		}
});

ts.AddTest("sample", function(ut)
{
	var a1 = [0,1,2,3,4];
	
	Random.seed();
	for (var x = 0; x < 20; x++)
		{
		var aSel = Random.sample(a1, 5);
		ut.AssertEq(aSel.length, 5);
		var m = {};
		for (var i = 0; i < a1.length; i++)
			{
			ut.AssertEq(m[aSel[i]], undefined);
			m[aSel[i]] = true;
			}
		for (var i = 0; i < a1.length; i++)
			ut.Assert(m[i]);
		}
		
	aSel = Random.sample(a1, 3);
	ut.AssertEq(aSel.length, 3);
});

ts.AddTest("shuffle", function(ut)
{
	var a1 = [0,1,2,3,4];
	
	Random.seed();
	
	for (x = 0; x < 20; x++)
		{
		Random.shuffle(a1);
		ut.AssertEq(a1.length, 5);
		var m = {};
		for (var i = 0; i < a1.length; i++)
			{
			ut.AssertEq(m[a1[i]], undefined);
			m[a1[i]] = true;
			}
		for (var i = 0; i < a1.length; i++)
			ut.Assert(m[i]);
		}
});

ts.Run();
ts.Report();

</script>
</body>
</html>
